﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using ZealousTTS.Common.Storage;
using System.Drawing;

namespace ZealousTTS.Web.Utility
{
    public static class ImageManager
    {
        private static readonly string _ProductImagePath = HttpContext.Current.Server.MapPath(AppConstants.Directories.PRODUCT_IMAGES);
        private static readonly string _CompanyLogoPath = HttpContext.Current.Server.MapPath(AppConstants.Directories.COMPANY_LOGO);
        private static readonly string _TempPath = HttpContext.Current.Server.MapPath(AppConstants.Directories.TEMP);

        public static void DeleteProductImages(string filename)
        {
            String fullPath = Path.Combine(_ProductImagePath, filename);
            if(File.Exists(fullPath))
                File.Delete(fullPath);

            ///Delete the Thumb
            String thumbFileName = String.Format("{0}_Thumb{1}", Path.GetFileNameWithoutExtension(filename), Path.GetExtension(filename));
            fullPath = Path.Combine(_ProductImagePath, thumbFileName);
            if (File.Exists(fullPath))
                File.Delete(fullPath);
        }

        public static void SaveThumbnail(string sourceFilename, Rectangle rectangle, int imageID, bool deleteSourceFile, string requestFrom)
        {
            using (var image = Image.FromFile(sourceFilename))
            {
                var img = Imaging.Crop(image, rectangle);
                Image thumbImage = Imaging.Resize(img, 200, 150, true);
                //var small = Imaging.Resize(img, 100, 75, true);
                //var mini = Imaging.Resize(img, 45, 34, true);
                String thumbFileName = Path.Combine((requestFrom == "ProductImage") ? _ProductImagePath : _CompanyLogoPath, String.Format("{0}_Thumb.jpg", imageID));
                Imaging.SaveJpeg(thumbFileName, thumbImage);
                thumbImage.Dispose();
                img.Dispose();
                image.Dispose();

                ///Move the Original Image
                MoveFile(sourceFilename, Path.Combine((requestFrom == "ProductImage") ? _ProductImagePath : _CompanyLogoPath, String.Format("{0}.jpg", imageID)));
                if (deleteSourceFile)
                {
                    File.Delete(sourceFilename);
                }                
            }
        }
        /// <summary>
        /// Moves a Given file to a destination. Overrites existing file.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        public static void MoveFile(String source, String destination)
        {
            if (File.Exists(source))
            {
                File.Move(source, destination);
            }
        }

        //public static void DeleteFile()
        //{
        //    string thumbSource = Path.Combine(_CompanyLogoPath, String.Format("{0}_Thumb.jpg", SessionCache.CurrentUser.Company.ID));
        //    string source = Path.Combine(_CompanyLogoPath, String.Format("{0}.jpg", SessionCache.CurrentUser.Company.ID));

        //    if (File.Exists(thumbSource))
        //    {
        //        File.Delete(thumbSource);
        //    }

        //    if (File.Exists(source))
        //    {
        //        File.Delete(source);
        //    }
        //}

        public static string SaveTempJpeg(Stream inputStream, out int w, out int h)
        {
            var g = Guid.NewGuid() + ".jpg";
            if (!Directory.Exists(_TempPath))
                Directory.CreateDirectory(_TempPath);

            var filePath = Path.Combine(_TempPath, g);

            using (var image = Image.FromStream(inputStream))
            {
                var resized = Imaging.Resize(image, 533, 400, true);
                Imaging.SaveJpeg(filePath, resized);                
                w = resized.Width;
                h = resized.Height;
                resized.Dispose();
                image.Dispose();
                return g;
            }
        }
        /// <summary>
        /// Gets the Tempurary Image Url
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static String GetTempImageUrl(string fileName)
        {
            return String.Format("{0}/{1}", AppConstants.Directories.TEMP, fileName);
        }
        /// <summary>
        /// Gets the ProductImageUrl
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static String GetProductImageUrl(string fileName)
        {
            return String.Format("{0}/{1}", AppConstants.Directories.PRODUCT_IMAGES, fileName);
        }
        /// <summary>
        /// Gets the LogoUrl
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static String GetLogoUrl(string fileName)
        {
            return String.Format("{0}/{1}", AppConstants.Directories.COMPANY_LOGO, fileName);
        }
    }
}